//build: 20240525
import { alsaClass } from './libvbar-m-dxalsa.so'
import dxCommon from './dxCommon.js'
const alsaObj = new alsaClass();
const alsa = {}

/**
 * alsa initialization
 * @param {string} id Handle ID, optional (a unique ID is required if initializing multiple instances).
 * @param {number} volume Volume (optional)
 * @param {number} periodSize Period size (optional)
 * @param {number} bufferSize Cache size (optional)
 * @returns handle id
 */
alsa.init = function (id, volume, periodSize, bufferSize) {
	if (volume === undefined || volume === null) {
		volume = 35
	}
	if (periodSize === undefined || periodSize === null) {
		periodSize = 512
	}
	if (bufferSize === undefined || bufferSize === null) {
		bufferSize = 2048
	}
	let pointer = alsaObj.alsaInit(volume, periodSize, bufferSize)
	if (!pointer) {
		throw new Error("alsa.init: init failed")
	}
	return dxCommon.handleId("alsa", id, pointer)
}

/**
 * alsa deinitialization
 * @param {string} id Handle ID, optional (must be consistent with the ID in init).
 * @returns true/false
 */
alsa.deinit = function (id) {
	let pointer = dxCommon.handleId("alsa", id)
	return alsaObj.alsaDeinit(pointer)
}

/**
 * Play music files
 * @param {string} path The absolute path to the WAV file, starting with '/app/code/', is required. It's typically placed in the project's resource directory (at the same level as src).
 * @param {string} id Handle ID, optional (must be consistent with the ID in init).
 * @returns true/false
 */
alsa.play = function (path, id) {
	if (!path) {
		throw new Error("alsa.play: 'path' parameter should not be null")
	}
	let pointer = dxCommon.handleId("alsa", id)
	return alsaObj.alsaWav(pointer, path)
}

/**
 * TTS (Text-to-Speech)
 * @param {string} The text to be played (required)
 * @param {string} id Handle ID, optional (must be consistent with the ID in init).
 * @returns true/false
 */
alsa.ttsPlay = function (str, id) {
	if (!str) {
		throw new Error("alsa.ttsPlay: 'str' parameter should not be null")
	}
	let pointer = dxCommon.handleId("alsa", id)
	return alsaObj.alsaAudioPlayString(pointer, str)
}

/**
 * Get volume
 * @param {string} id Handle ID, optional (must be consistent with the ID in init).
 * @returns Returns a numeric volume value, which will not exceed the volume range.
 */
alsa.getVolume = function (id) {
	let pointer = dxCommon.handleId("alsa", id)
	return alsaObj.alsaGetVolume(pointer)
}

/**
 * Setting the volume too high or too low will default to the maximum or minimum volume range.
 * @param {number} volume Volume (required)
 * @param {string} id Handle ID, optional (must be consistent with the ID in init).
 * @returns true/false
 */
alsa.setVolume = function (volume, id) {
	let pointer = dxCommon.handleId("alsa", id)
	return alsaObj.alsaSetVolume(pointer, volume)
}
/**
 * Play streaming audio
 * @param {ArrayBuffer} Audio stream (required)
 * @param {string} id Handle ID, optional (must be consistent with the ID in init).
 * @returns true/false
 */
alsa.alsaWavData = function (buffer, id) {
	if (!buffer) {
		throw new Error("alsa.alsaWavData: 'buffer' parameter should not be null")
	}
	let pointer = dxCommon.handleId("alsa", id)
	return alsaObj.alsaWavData(pointer, buffer)
}

export default alsa;
